/*
 * @Author: zhangxiao
 * @Email: zhangxiao0125@163.com
 * @Date: 2020-05-28 16:28:50
 * @Last Modified by: zhangxiao
 * @Last Modified time: 2020-07-03 10:21:33
 * @Description: 用户登录
 */

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_code_framework/base/base_main_tabbar.dart';
import 'package:flutter_code_framework/base/base_widget.dart';
import 'package:flutter_code_framework/model_data/user/data.dart';
import 'package:flutter_code_framework/route/route_transition.dart';
import 'package:flutter_code_framework/utils/global.dart';
import 'package:flutter_sfsymbols/flutter_sfsymbols.dart';

class Login extends BaseWidget {
  Login({Key key}) : super(key: key);

  @override
  BaseWidgetState<BaseWidget> getState() {
    return _LoginState();
  }
}

class _LoginState extends BaseWidgetState<Login> {
  GlobalKey<FormState> _loginKey = GlobalKey<FormState>(); //Form
  TextEditingController _userName = TextEditingController(); //用户名
  TextEditingController _userPwd = TextEditingController(); //密码

  bool _passwordVisible;
  bool _obscureText;
  bool _setCodeValue=false;
  @override
  Widget buildWidget(BuildContext context) {
   
      if(!_setCodeValue){
      G.user.getString().then((value) => _getUserInfo(value));
     }
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          margin: EdgeInsets.only(left: 24.0, top: 64.0),
          child: Text('账号密码登陆',
              style: TextStyle(
                  color: Theme.of(context).primaryTextTheme.headline4.color,
                  fontSize: 24,
                  decoration: TextDecoration.none)),
        ),
        Container(
          margin: EdgeInsets.only(left: 24.0, top: 16.0),
          child: Text('请输入平台统一分配的账号密码',
              style: TextStyle(
                  color: Theme.of(context).primaryTextTheme.caption.color,
                  fontSize: 16.0)),
        ),
        Form(
          key: _loginKey,
          child:
              Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
            Container(
              margin: EdgeInsets.only(left: 24.0, top: 24.0),
              child: Text('登录名/身份证号',
                  style: TextStyle(
                      color: Theme.of(context).primaryTextTheme.caption.color,
                      fontSize: 14.0)),
            ),
            Container(
                margin: EdgeInsets.only(left: 24.0, top: 15.0, right: 24.0),
                child: TextFormField(
                  controller: _userName,
                  decoration: InputDecoration(
                      enabledBorder: UnderlineInputBorder(
                        borderSide: BorderSide(
                            color: Color.fromARGB(255, 187, 187, 187)),
                      ),
                     focusedBorder: UnderlineInputBorder(
                        borderSide: BorderSide(
                            color: Color.fromARGB(255, 187, 187, 187)),
                      )),
                )),
            Container(
                margin: EdgeInsets.only(left: 24.0, top: 11.0),
                child: Text('密码',
                    style: TextStyle(
                        color: Theme.of(context).primaryTextTheme.caption.color,
                        fontSize: 14.0))),
            Container(
              margin: EdgeInsets.only(left: 24.0, top: 15.0, right: 24.0),
              child: TextFormField(
                  controller: _userPwd,
                  obscureText: _obscureText,
                  decoration: InputDecoration(
                    enabledBorder: UnderlineInputBorder(
                      borderSide:
                          BorderSide(color: Color.fromARGB(255, 187, 187, 187)),
                    ),
                    focusedBorder: UnderlineInputBorder(
                      borderSide:
                          BorderSide(color: Color.fromARGB(255, 187, 187, 187)),
                    ),
                    suffixIcon: IconButton(
                        icon: Icon(
                            _passwordVisible
                                ? Icons.visibility
                                : Icons.visibility_off,
                            color: Theme.of(context).primaryColor),
                        onPressed: () {
                          setState(() {
                            _passwordVisible = !_passwordVisible;
                            _obscureText = !_passwordVisible;
                           
                          });
                        }),
                  )),
            ),
            Container(
                margin: EdgeInsets.only(left: 24.0, top: 44.0, right: 24.0),
                child: GestureDetector(
                  child: Container(
                    height: 46,
                    width: double.infinity,
                    decoration: BoxDecoration(
                        color: Theme.of(context).primaryColor,
                        borderRadius: BorderRadius.circular(5.0)),
                    child: Center(
                      child: Text(
                        '登           录',
                        style: TextStyle(
                            color:
                                Theme.of(context).primaryTextTheme.button.color,
                            fontSize: 16),
                      ),
                    ),
                  ),
                  onTap: () async {
                     FocusScope.of(context).requestFocus(FocusNode());
                    if (_userName.text.trim().isEmpty) {
                      showRightToast('请输入登陆名或身份证号');
                      return;
                    }
                    if (_userPwd.text.trim().isEmpty) {
                      showRightToast('请输入登陆密码');
                      return;
                    }
                    var rs = await G.req.user.login(code: _userName.text, password: _userPwd.text);
                    if (rs.data['isOK'] != null) {
                      showRightToast(rs.data['errMsg']);
                      return;
                    }
                    if (G.user.data.guideFlag == 1) {
                      Navigator.pushNamed(context, '/user_guide');
                    } else {
                      RouteTransition(BaseMainTabbarPage())
                          .pushAndRemoveUntilTransRoute(context);
                    }
                  },
                ))
          ]),
        ),
        Expanded(
            child: Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: <Widget>[
            Expanded(
              child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.only(left: 24.0),
                      child: Icon(SFSymbols.info_circle_fill,
                          color: Color.fromRGBO(236, 115, 100, 1), size: 14),
                    ),
                    Expanded(
                        child: Container(
                            margin: EdgeInsets.only(left: 3.0, bottom: 60, right: 24.0),
                            child: Text(
                                '平台暂不提供开放注册功能。如对帐号信息存在疑问或忘记密码，请联系学校或平台客服。',
                                style: TextStyle(
                                    color: Theme.of(context)
                                        .primaryTextTheme
                                        .caption
                                        .color,
                                    fontSize: 12.0),
                                softWrap: true)))
                  ]),
            )
          ],
        ))
      ],
    );
  }

  @override
  void initState() {
    super.initState();
   
    _passwordVisible = false;
    _obscureText = true;
  }

  @override
  void onCreate() {
    // log("Login onCreate");
  }

  @override
  void onPause() {
    // log('onPause');
  }

  @override
  void onResume() {
    // log("onResume");
  }
  
  //赋值登陆用户名
  _getUserInfo(dynamic value){
      if(value!=null){
      setState(() {
       _setCodeValue=true;
      });
      UserData _userData=  UserData.fromJson(json.decode(value.toString()));
      _userName.value = _userName.value.copyWith(text: _userData.code);
    }
  }
}
